module("MainMenuUI", package.seeall)

function create()
	local self = ccs.GUIReader:getInstance():widgetFromJsonFile("res/UI/MainUI/MainMenu.ExportJson")
	init(self)
	return self
end

function onButtonFolderClick(self)
	local dt = 0.2
	local distance = 100
	if (self._folderState == "opened") then
		self._folderState = "closiing"
		for k,btn in ipairs(self._buttonH) do
			local actionMove = cc.MoveBy:create(dt, cc.p(distance, 0))
			local actionFadeout = cc.FadeOut:create(dt)
			local actionSpawn = cc.Spawn:create(actionMove, actionFadeout)
			local actionFunc = cc.CallFunc:create(function() self._folderState = "closed" end)
			local actionSeq = cc.Sequence:create(actionSpawn, cc.Hide:create(),actionFunc)
			btn:runAction(actionSeq)
		end

		for k,btn in ipairs(self._buttonV) do
			local actionMove = cc.MoveBy:create(dt, cc.p(0, -distance))
			local actionFadeout = cc.FadeOut:create(dt)
			local actionSpawn = cc.Spawn:create(actionMove, actionFadeout)
			local actionSeq = cc.Sequence:create(actionSpawn, cc.Hide:create())
			btn:runAction(actionSeq)
		end
	elseif (self._folderState == "closed") then
		self._folderState = "opening"
		for k,btn in ipairs(self._buttonH) do
			local actionMove = cc.MoveBy:create(dt, cc.p(-distance, 0))
			local actionFadein = cc.FadeIn:create(dt)
			local actionSpawn = cc.Spawn:create(actionMove, actionFadein)
			local actionFunc = cc.CallFunc:create(function() self._folderState = "opened" end)
			local actionSeq = cc.Sequence:create(cc.Show:create(), actionSpawn, actionFunc)
			btn:runAction(actionSeq)
		end

		for k,btn in ipairs(self._buttonV) do
			local actionMove = cc.MoveBy:create(dt, cc.p(0, distance))
			local actionFadein = cc.FadeIn:create(dt)
			local actionSpawn = cc.Spawn:create(actionMove, actionFadein)
			local actionSeq = cc.Sequence:create(cc.Show:create(), actionSpawn)
			btn:runAction(actionSeq)
		end
	end
end

function onButtonMenuClick(self, sender)
	if (sender:getName() == "Button_H1") then
		SceneManager.switchToScene(MapScene.create())
	end
end

function init(self)
	self._buttonFolder = self:getChildByName("Button_Folder")
	self._panelVertical = self:getChildByName("Panel_Vertical")
	self._panelHorizontal = self:getChildByName("Panel_Horizontal")

	self._buttonH = {}
	self._buttonV = {}

	self._folderState = "opened"

	for i=1,5 do
		self._buttonH[i] = self._panelHorizontal:getChildByTag(i)
		if (self._buttonH[i]) then
			self._buttonH[i]:addTouchEventListener(
				function(sender, eventType)
					if (eventType == ccui.TouchEventType.ended) then
						onButtonMenuClick(self, sender)
					end
				end
			)
		end
	end

	for i=1,4 do
		self._buttonV[i] = self._panelVertical:getChildByTag(i)
		if (self._buttonV[i]) then
			self._buttonV[i]:addTouchEventListener(
				function(sender, eventType)
					if (eventType == ccui.TouchEventType.ended) then
						onButtonMenuClick(self, sender)
					end
				end
			)
		end
	end

	self._buttonFolder:addTouchEventListener(
		function(sender, eventType)
			if (eventType == ccui.TouchEventType.ended) then
				onButtonFolderClick(self)
			end
		end)
end
